# config_manager.py
import configparser
import os


class ConfigManager:
    def __init__(self, config_file='config.ini'):
        self.config_file = config_file
        self.config = configparser.ConfigParser()
        self.load_config()

    def load_config(self):
        """加载配置文件，如果不存在则创建默认配置"""
        if os.path.exists(self.config_file):
            self.config.read(self.config_file, encoding='utf-8')

        # 确保基本配置部分存在
        if 'Servers' not in self.config:
            self.config['Servers'] = {}

        # 设置默认值（如果不存在）
        Servers_section = self.config['Servers']
        if 'Servers_path' not in Servers_section:
            Servers_section['servers_path'] = ''
        if 'servers_columns' not in Servers_section:
            Servers_section['servers_columns'] = "分类,路段,堡垒机,跳机,密码,前置机,密码,超融合LinuxIP,Origin_IP,Gate_IP,超融合Linux密码,超融合Windows密码,站级密码"


    def save_config(self):
        """保存配置到文件"""
        with open(self.config_file, 'w', encoding='utf-8') as f:
            self.config.write(f)

    def get_servers_path(self):
        """获取Servers可执行文件路径"""
        return self.config['Servers']['servers_path']

    def set_servers_path(self, path):
        """设置Servers可执行文件路径"""
        self.config['Servers']['servers_path'] = path
        self.save_config()

    def get_servers_columns(self) -> list:
        """获取路段服务器表格列名列表"""
        columns_str = self.config['Servers'].get('servers_columns', "分类,路段,堡垒机,跳机,密码,前置机,密码,超融合LinuxIP,Origin_IP,Gate_IP,超融合Linux密码,超融合Windows密码,站级密码")
        return [col.strip() for col in columns_str.split(',')]

    def set_servers_columns(self, columns: list):
        """设置路段服务器表格列名列表"""
        self.config['Servers']['servers_columns'] = ",".join(columns)
        self.save_config()

